package edu.avans.hartigehap.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.data.domain.Sort;

import edu.avans.hartigehap.domain.Order;
import edu.avans.hartigehap.domain.Restaurant;

// this impl class must be in the same package as the interfaces
// you could call this a spring limitation

public class OrderRepositoryImpl implements OrderRepositoryCustom {

	@PersistenceContext
	private EntityManager em;

	// this is a custom method for which Spring cannot create an implementation
	// so we need to make our own repository implementation!
	public List<Order> findSubmittedOrdersForRestaurant(Restaurant restaurant) {
		List<Order> orders = em.createNamedQuery("Order.findSubmittedLocalOrders", Order.class).
				setParameter("restaurant", restaurant).getResultList();
		orders.addAll(em.createNamedQuery("Order.findSubmittedInternetOrders", Order.class).
				setParameter("restaurant", restaurant).getResultList());
		return orders;
	}

	//Added
	public List<Order> findByOrderStatusAndBillDiningTableRestaurant(
			Order.OrderStatus orderStatus, 
			Restaurant restaurant, 
			Sort sort){
		List<Order> orders = (em.createNamedQuery("Order.findByLocalOrderStatusAndBillRestaurant", Order.class)
				.setParameter("orderStatus", orderStatus)
				.setParameter("restaurant", restaurant))
				.getResultList();
		orders.addAll((em.createNamedQuery("Order.findByInternetOrderStatusAndBillRestaurant", Order.class)
				.setParameter("orderStatus", orderStatus)
				.setParameter("restaurant", restaurant))
				.getResultList());
		return orders;
	}


}
